<?php

namespace App\Http\Controllers\Client\Member;

use App\Models\Ass;
use App\Models\AssMember;
use App\Models\Member\Member;
use App\Models\Work;
use App\Services\Com\UtilService;
use Illuminate\Http\Request;
use App\Http\Controllers\RootController as Controller;
use Illuminate\Support\Facades\DB;

class AssAddController extends Controller
{
    public function handle(Request $request, Member $model)
    {
        /******参数 start******/
        $param['id'] = (int)$request->input('id', '');
        $param['obj_id'] = (int)$request->input('obj_id', '');
        $param['member_ids'] = (array)$request->input('member_ids', []);
        $param['lat'] = trim($request->input('lat', ''));
        $param['lng'] = trim($request->input('lng', ''));
        $param['content'] = trim($request->input('content', ''));
        $param['imgs'] = (array)($request->input('imgs', []));
        $param['address'] = trim($request->input('address', ''));
        $param['type'] = trim($request->input('type', ''));
        $lastAss = Ass::orderBy('id', 'desc')->first();
        $param['no'] = 10000000 + (empty($lastAss) ? 1 : $lastAss->id);
        $param['member_id'] = $this->id();
        $param['org_id'] = $this->user()->org_id;
        $param['work_org_id'] = (int)$request->input('work_org_id', 0);

        $rules    = [
            'content' => 'required',
            'imgs' => 'required',
            'type' => 'required',
        ];
        $messages = [
            'content.required'                   => '请输入内容',
            'imgs.required'                   => '请上传图片',
            'type.required'                   => '请选择上传类别',
        ];

        if (true !== ($error = $this->validator($param, $rules, $messages)))
        {
            return $this->failure($error, 'VALIDATION_FAIL');
        }

        try
        {
            $param['imgs'] = json_encode($param['imgs']);
            DB::beginTransaction();

            $member_ids = array_merge([$this->id()], $param['member_ids']);
            $member_names = Member::whereIn('id', $member_ids)->pluck('name')->toArray();
            $param['member_names'] = implode(',', $member_names);

            $row = $param;

            unset($row['member_ids']);

            if(!empty($param['id'])){
                $ass = Ass::find($param['id']);

                $ass->update($row);

                AssMember::where('ass_id', $param['id'])->delete();
            }else{
                $ass = Ass::create($row);

                $work = Work::where('year', date('Y'))->where('member_id', $this->id())->first();
                if($param['type'] == 1 && !empty($work) && $work->finish < $work->target){
                    $work->finish = $work->finish + 1;
                    $work->save();
                }
            }


            AssMember::create([
                'ass_id' => $ass->id,
                'member_id' =>  $this->id(),
                'org_id' => $this->user()->org_id,
                'type' => 1,
            ]);

            if(!empty($param['member_ids'])){
                foreach ($param['member_ids'] as $member_id){
                    AssMember::create([
                        'ass_id' => $ass->id,
                        'member_id' => $member_id,
                        'org_id' => $this->user()->org_id,
                        'type' => 0,
                    ]);
                }
            }


            DB::commit();
        }
        catch (\Exception $e)
        {
            DB::rollBack();
            return $this->failure($e->getMessage(), 'E_DB');
        }

        return $this->success();
    }
}
